package com.myapp.ruyixing.ui;

import static com.myapp.ruyixing.Config.sCityName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.Config;
import com.myapp.ruyixing.R;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class MoreFragment extends Fragment implements OnClickListener {

	public MoreFragment() {
		// Required empty public constructor
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.fragment_more, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		view.findViewById(R.id.layout_about_us).setOnClickListener(this);
		view.findViewById(R.id.layout_change_city).setOnClickListener(this);
		view.findViewById(R.id.layout_system_settings).setOnClickListener(this);
		view.findViewById(R.id.layout_feedback).setOnClickListener(this);
		view.findViewById(R.id.layout_add_shop).setOnClickListener(this);
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onResume() {
		setCityName(sCityName);
		super.onResume();
	}

	private void showToast(String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.layout_about_us:
			intent = new Intent(getActivity(), AboutActivity.class);
			startActivity(intent);
			break;
		case R.id.layout_change_city:
			if (Config.isShopManagerVersion(getActivity())) {
				showToast("暂无法在客户端修改城市哦！");
			} else {
				startActivity(new Intent(getActivity(), CityListActivity.class));
			}
			break;
		case R.id.layout_feedback:
			intent = new Intent(getActivity(), FeedbackActivity.class);
			startActivity(intent);

			break;
		case R.id.layout_system_settings:
			intent = new Intent(getActivity(), SettingsActivity.class);
			startActivity(intent);

			break;
		case R.id.layout_add_shop:
			if (Config.isShopManagerVersionAndNotBinded(getActivity())) {
				showToast("您未绑定会员帐号，请绑定后再使用此功能");
			} else {
				intent = new Intent(getActivity(), AddShopActivity.class);
				startActivity(intent);
			}

			break;

		default:
			break;
		}
	}

	public void setCityName(String city) {
		if (TextUtils.isEmpty(city)) {
			return;
		}
		int index = city.lastIndexOf("市");
		if (index > 0)
			city = city.substring(0, index);
		((TextView) (getView().findViewById(R.id.text_city))).setText(city);
	}
}
